package com.pureut.storage.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.AndroidHandoverPdaDto;
import com.pureut.storage.dto.AndroidPdaDto;
import com.pureut.storage.export.PurchaseReceiptExport;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import com.pureut.system.api.dto.PurchaseOrderDto;
import com.pureut.storage.dto.PurchaseReceiptDto;
import com.pureut.storage.service.PurchaseReceiptService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 采购入库控制层
 * Author:C
 * Date:2022/12/23 10:16
 * @DESC
 */
@RestController
@RequestMapping("/purchase-receipt")
public class PurchaseReceiptController extends BaseController {

    @Resource
    PurchaseReceiptService purchaseReceiptService;

    /**
     * 获取采购入库分页列表
     */
    @RequiresPermissions("warehouse:warehousing:purchase:list")
    @GetMapping("/list")
    public TableDataInfo getList(PurchaseReceiptVo purchaseReceiptVo) {
        startPage();
        //查询采购入库列表
        List<PurchaseReceiptDto> list = purchaseReceiptService.getReceiptList(purchaseReceiptVo);
        return getDataTable(list);
    }

    /**
     * 新增采购入库
     */
    @RequiresPermissions("warehouse:warehousing:purchase:add")
    @Log(title = "新增采购入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody PurchaseReceiptVo purchaseReceiptVo) throws Exception {
        return toAjax(purchaseReceiptService.addReceipt(purchaseReceiptVo));
    }

    /**
     * 批次到货入库
     */
    @Log(title = "批次到货入库", businessType = BusinessType.INSERT)
    @PostMapping("/batchArrival")
    public AjaxResult batchArrival(@RequestBody PurchaseOrderDto purchaseOrderDto) throws Exception {
        return AjaxResult.success(purchaseReceiptService.batchArrival(purchaseOrderDto));
    }

    /**
     * 关结采购入库
     */
    @RequiresPermissions("warehouse:warehousing:purchase:closing")
    @Log(title = "关结采购入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody PurchaseReceiptVo purchaseReceiptVo) {
        return toAjax(purchaseReceiptService.close(purchaseReceiptVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("warehouse:warehousing:purchase:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(purchaseReceiptService.getView(id));
    }

    /**
     * 修改采购入库
     */
    @RequiresPermissions("warehouse:warehousing:purchase:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateReceipt(@Validated @RequestBody PurchaseReceiptVo purchaseReceiptVo) {
        return toAjax(purchaseReceiptService.updateReceipt(purchaseReceiptVo));
    }

    /**
     * 删除
     */
    @RequiresPermissions("warehouse:warehousing:purchase:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public AjaxResult deleteReceipt(@PathVariable("idStr") String idStr) {
        return toAjax(purchaseReceiptService.deleteReceipt(idStr));
    }

    /**
     * 入库/送检
     */
    @RequiresPermissions("warehouse:warehousing:purchase:deliver")
    @Log(title = "入库/送检", businessType = BusinessType.INSERT)
    @PostMapping("/warehousInspection")
    public AjaxResult warehousInspection(@RequestBody WarehousInspectionVo warehousInspectionVo) throws Exception {
        return toAjax(purchaseReceiptService.warehousInspection(warehousInspectionVo));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:warehousing:purchase:export")
    @PostMapping("/inspectionMessageExport")
    public AjaxResult inspectionMessageExport(HttpServletResponse response, PurchaseReceiptVo purchaseReceiptVo) throws IOException {
        List<PurchaseReceiptExport> infoExport = purchaseReceiptService.getInfoExport(purchaseReceiptVo);
        ExcelUtil<PurchaseReceiptExport> excelUtil = new ExcelUtil<PurchaseReceiptExport>(PurchaseReceiptExport.class);
        excelUtil.excelWriter(response, infoExport, "采购入库信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 采购入库（pda）
     */
    @RequiresPermissions("warehouse:warehousing:purchase:goStorage")
    @PostMapping("/warehousingPDA")
    @Log(title = "采购入库(pda)", businessType = BusinessType.UPDATE)
    public AjaxResult warehousingPda(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return AjaxResult.success(purchaseReceiptService.warehousingPda(pickingRestDbVo));
    }

    /**
     * 根据采购订单状态修改采购入库状态(feign调用)
     */
    @PostMapping("/updateReceiptStatus")
    public AjaxResult updateReceiptStatus(@RequestBody UpdateReceiptStatusVo updateReceiptStatusVo) {
        return AjaxResult.success(purchaseReceiptService.updateReceiptStatus(updateReceiptStatusVo));
    }

    /**
     * 手持pda 入库列表
     */
    @GetMapping("/androidPdaList")
    public TableDataInfo androidPdaList(int number) {
        startPage();
        //查询采购入库列表
        List<AndroidPdaDto> list = purchaseReceiptService.androidPdaList(number);
        return getDataTable(list);
    }

    /**
     * 手持pda 查看接口
     */
    @GetMapping("/androidPdaView")
    public AjaxResult androidPdaView(int number, long id) {
        return AjaxResult.success(purchaseReceiptService.androidPdaView(number, id));
    }

    /**
     * 手持pda 拣货撤销列表
     */
    @GetMapping("/pickCancellationList")
    public TableDataInfo pickCancellationList(int number) {
        startPage();
        //查询采购入库列表
        List<AndroidPdaDto> list = purchaseReceiptService.pickCancellationList(number);
        return getDataTable(list);
    }

    /**
     * 手持pda 拣货完成
     */
    @PostMapping("/completionPickingAndroid")
    public AjaxResult completionPickingAndroid(@RequestBody CompletionPickingVo completionPickingVo) throws Exception {
        return AjaxResult.success(purchaseReceiptService.completionPickingAndroid(completionPickingVo));
    }

    /**
     * 手持pda 拣货撤销
     */
    @PostMapping("/pickCancellationAndroid")
    public AjaxResult pickCancellationAndroid(@RequestBody CompletionPickingVo completionPickingVo) {
        return AjaxResult.success(purchaseReceiptService.pickCancellationAndroid(completionPickingVo));
    }

    /**
     * 手持pda 交接列表
     */
    @GetMapping("/androidHandoverList")
    public TableDataInfo androidHandoverList(int number) {
        startPage();
        //查询交接列表
        List<AndroidHandoverPdaDto> list = purchaseReceiptService.androidHandoverList(number);
        return getDataTable(list);
    }

    /**
     * 手持pda 交接详情
     */
    @GetMapping("/androidHandoverView")
    public AjaxResult androidHandoverView(int number, long id) {
        return AjaxResult.success(purchaseReceiptService.androidHandoverView(number, id));
    }

    /**
     * 手持pda 交接
     */
    @PostMapping("/androidHandover")
    public AjaxResult androidHandover(@RequestBody AndroidHandoverVo androidHandoverVo) {
        return AjaxResult.success(purchaseReceiptService.androidHandover(androidHandoverVo));
    }

}
